// pages/home/order-list/index.js
import { request, baseurl } from '../../../utils/index.js';
import { queryOrder } from '../../../api/order'
const dayjs = require('dayjs')

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    status: String,
    timeStart: String,
    timeEnd: String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    orderList: [],
    triggered: false, // 设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
    allPage: 1, // 总页数
    curPage: 1, // 当前页数
    windowHeight: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 加载列表
    loadData: function () {
      const {status, curPage, timeStart, timeEnd} = this.data
      return queryOrder({
        ordState: status,
        pageNum: curPage,
        delyStartTime: timeStart || undefined,
        delyEndTimeEnd: timeEnd || undefined,
        pageSize: 10,
      })
    },

    // 上拉到底部触发
    loadMore: function () {
      console.log('loadMore')
      const {curPage, orderList} = this.data
      this.data.curPage++;
      this.loadData().then(res => {
        if(res.data.list && res.data.list.length && this.data.curPage <= res.data.pages) {
          this.setData({
            orderList: orderList.concat(res.data.list),
            allPage: res.data.pages || 1,
          })
        } else {
          this.data.curPage--;
        }
      })
    },

    // 下拉刷新
    onRefresh() {
      var self = this;

      this.data.curPage = 1;
      this.setData({
        triggered: true, // 将triggered属性设置为true，表示下拉刷新已经被触发
      })
      this.loadData().then(res => {
        this.setData({
          orderList: res.data.list || [],
          allPage: res.data.pages || 1,
          curPage: 1,
          triggered: false, // 将triggered属性设置为false，表示下拉刷新已完成
        })
        wx.showToast({
          title: "刷新成功"
        })
      })

      wx.$event.emit('e-load-order-count');
    },
    // 刷新列表
    refreshList() {
      this.data.curPage = 1;
      this.loadData().then(res => {
        this.setData({
          orderList: res.data.list || [],
          allPage: res.data.pages || 1,
        })
      })

      wx.$event.emit('e-load-order-count');
    },
    // 置顶
    onToTop(e) {
      const index = e.detail
      const {orderList} = this.data
      const items = orderList.splice(index, 1)
      orderList.splice(0,0,items[0])
      this.setData({
        orderList: orderList
      })
      console.log(index)
    }
  },

  pageLifetimes: {
    show: function () {
      const page = this;
      // 获取屏幕高度
      wx.getSystemInfo({
        success: function (res) {
          page.setData({
            windowHeight: res.windowHeight - 60
          })
          console.log('屏幕高度：', res.windowHeight);
        }
      })
    },
  }
})